/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Chatree Siangklom
 * Created On : Feb 4, 2013  11:38:15 AM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.std.batch.common
 * File Name : CustomerBatch.java
 * TODO
 */
package com.tis.std.batch.common;

import java.util.List;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import promis.utility.QueueProcessAction;
import apps.sessionFactory;

import com.tis.std.commons;

/**
 * @author chatree
 *
 */
public class CustomerBatch extends commons {

	private static final Logger log = Logger.getLogger(ProjectBatch.class);
	private static final long MEGABYTE = 1024L * 1024L;

	public static long bytesToMegabytes(long bytes) {		  		  
		  return (bytes / MEGABYTE);
	}

	private void BatchExec() throws Exception {
		try{
			
			StringBuilder _strQuery = new StringBuilder();
			_strQuery.append(" SELECT CUSTOMER_CODE, STATUS FROM SPL.VW_SYNC_MKT_CUSTOMER GROUP BY CUSTOMER_CODE, STATUS ORDER BY CUSTOMER_CODE, STATUS ");
			/*
			X.CONTR_ROAD_DESC  IS NOT NULL 
            OR X.BILLING_ROAD_DESC IS NOT NULL 
            OR X.ID_ROAD_DESC IS NOT NULL
			*/
			
			List<Object[]> result = (List<Object[]>) sessionFactory.CreateEmPromis().createNativeQuery(_strQuery.toString()).getResultList();
			System.out.println("result size =" + CommonService.getSize(result));
			
			if(!CommonService.IsNull(result)){
				try{
					int i = 0;
					for (Object[] value : result) {						
						i++;
						if(i <= 10){
							CleanValue();						
							setCustomerCode(CommonService.IsNullRNull(value[0]));
							setStatus(CommonService.IsNullRNull(value[1]));
							
							setProcessAction(QueueProcessAction.BATCH);
							//setProcessActionBy(CommonService.IsNullRNull(value[7]));
							
							new apps.promis.out.common.SyncQueueAction().PutQueue(	promis.utility.BusinessModule.COMMONS.toString(), 
																					promis.utility.ProcessModule.CUSTOMER.toString(),																				null,
																					QueueProcessAction.BATCH,
																					ConvertParamToHashMap()
																					);	
						}else
							break;
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}catch(Exception e){
			log.error(e.getMessage());
			throw e;
		}
	}
	
	public static void main(String[] args) {
		try{
			
			new CustomerBatch().BatchExec();
			
		}catch(Exception e){
			log.error(e.getMessage());
		}
	}

}
